package com.itboye.shop.me;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.util.helper.utils.SuiJiCode;
import com.itboye.shop.util.loadutil.MAlert;

/**
 * 忘记密码1
 */
public class ForgetPwdActivity extends BaseActivity {
    ImageView back;
    TextView tvTitle;
    Button button_next;
    TextView forget_genghuan;
    ImageView forget_yzm;
    EditText edit_phone,edit_yzm;
    //产生的验证码
    private String realCode;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_forget_pass);
        tvTitle.setText("忘记密码");

        //将验证码用图片的形式显示出来
        forget_yzm.setImageBitmap(SuiJiCode.getInstance().createBitmap());
        realCode = SuiJiCode.getInstance().getCode().toLowerCase();

        //密码更新成功后的广播
        IntentFilter filter=new IntentFilter(ForgetPwdTwoActivity.UPDATE_PASS);
        registerReceiver(update,filter);
    }
    BroadcastReceiver update=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(update);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.forget_genghuan:
                forget_yzm.setImageBitmap(SuiJiCode.getInstance().createBitmap());
                realCode = SuiJiCode.getInstance().getCode().toLowerCase();
                break;
            case R.id.button_next:
                String phone=edit_phone.getText().toString();
                String phoneCode = edit_yzm.getText().toString().toLowerCase();
                String msg = "生成的验证码："+realCode+"输入的验证码:"+phoneCode;
                if (phoneCode.equals(realCode)) {
                    MAlert.alert("验证通过", Gravity.CENTER);
                    Intent intent01=new Intent(this,ForgetPwdTwoActivity.class);
                    intent01.putExtra("phone",phone);
                    startActivity(intent01);
                } else {
                    MAlert.alert("验证错误",Gravity.CENTER);
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }
}
